package com.spelet.simulation;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.utils.Array;
import com.spelet.StaticSprites;
import com.spelet.StaticTextures;
import com.spelet.StaticVariables;
import com.spelet.VariableCollection;
import com.spelet.screens.MapEditor;

public class ViewBar {
	float x = Gdx.graphics.getWidth()-200;
	float y = StaticVariables.barSize;
	int w = 200;
	int h = Gdx.graphics.getHeight();
	Sprite background;
	Sprite[] recentBrushesSprites;
	Array<Integer> recentBrushes;
	Rectangle[] rbCD;
	Sprite blocks;
	Sprite terrain;
	boolean renderingBlocks;
	boolean renderingTerrain;
	Rectangle bR;
	Rectangle tR;
	boolean walledEdges;
	public ViewBar() {
		renderingBlocks = true;
		renderingTerrain = true;
		walledEdges = false;
		blocks = StaticSprites.meBlocks;
		terrain = StaticSprites.meTerrain;
		rbCD = new Rectangle[3];
		recentBrushes = new Array<Integer>();
		recentBrushesSprites = new Sprite[4];
		background = StaticTextures.mapEdit.createSprite("viewbarbackground");
		background.setSize(background.getWidth(), Gdx.graphics.getHeight()-StaticVariables.barSize);
		recentBrushesSprites[0] = StaticSprites.ctbSprites[StaticVariables.grassVal];
		recentBrushesSprites[1] = StaticSprites.ctbSprites[StaticVariables.grassVal];
		recentBrushesSprites[2] = StaticSprites.ctbSprites[StaticVariables.grassVal];
		recentBrushesSprites[3] = StaticSprites.ctbSprites[StaticVariables.grassVal];
		recentBrushes.add(StaticVariables.grassVal);
		recentBrushes.add(StaticVariables.grassVal);
		recentBrushes.add(StaticVariables.grassVal);
		recentBrushes.add(StaticVariables.grassVal);

		rbCD[2] = new Rectangle(x+20,y+background.getHeight()-blocks.getHeight()-recentBrushesSprites[3].getHeight()*2-20,32,32);
		rbCD[1] = new Rectangle(x+20+40,y+background.getHeight()-blocks.getHeight()-recentBrushesSprites[3].getHeight()*2-20,32,32);
		rbCD[0] = new Rectangle(x+20+80,y+background.getHeight()-blocks.getHeight()-recentBrushesSprites[3].getHeight()*2-20,32,32);
		tR = new Rectangle(x+20, Gdx.graphics.getHeight()-terrain.getHeight()-20,64,64);
		bR = new Rectangle(x+background.getWidth()-blocks.getWidth()-20, Gdx.graphics.getHeight()-blocks.getHeight()-20,64,64);
	}
	public boolean hit(int x, int y) {
		System.out.println("Y: " + y);
		return background.getBoundingRectangle().contains(x,y);
	}
	public void pressed(MapEditor me, int x, int y) {
		for (int i = 0; i < 3; i++) {
			if (rbCD[i].contains(x,y)) {
				if (recentBrushes.get(i) == VariableCollection.grass) { 
					me.placingBlock = VariableCollection.nothing;
					me.placingTerrain = VariableCollection.grass;
				} else if (recentBrushes.get(i) == VariableCollection.hole) { 
					me.placingBlock = VariableCollection.nothing;
					me.placingTerrain = VariableCollection.hole;
				} else if (recentBrushes.get(i) == VariableCollection.dirt) { 
					me.placingBlock = VariableCollection.nothing;
					me.placingTerrain = VariableCollection.dirt;
				} else if (recentBrushes.get(i) == VariableCollection.wall) { 
					me.placingBlock = VariableCollection.wall;
					me.placingTerrain = VariableCollection.nothing;
				} else if (recentBrushes.get(i) == VariableCollection.door_red) { 
					me.placingBlock = VariableCollection.door_red;
					me.placingTerrain = VariableCollection.nothing;
				} else if (recentBrushes.get(i) == VariableCollection.key_red) { 
					me.placingBlock = VariableCollection.key_red;
					me.placingTerrain = VariableCollection.nothing;
				} else if (recentBrushes.get(i) == VariableCollection.moveBlock) { 
					me.placingBlock = VariableCollection.moveBlock;
					me.placingTerrain = VariableCollection.nothing;
				} else if (recentBrushes.get(i) == VariableCollection.boulder) { 
					me.placingBlock = VariableCollection.boulder;
					me.placingTerrain = VariableCollection.nothing;
				} else if (recentBrushes.get(i) == VariableCollection.CharDummy) {
					me.placingBlock = VariableCollection.CharDummy;
					me.placingTerrain = VariableCollection.nothing;
				} else if (recentBrushes.get(i) == VariableCollection.mergeblock) {
					me.placingBlock = VariableCollection.mergeblock;
					me.placingTerrain = VariableCollection.nothing;
				} else if (recentBrushes.get(i) == VariableCollection.key_blue) {
					me.placingBlock = VariableCollection.key_blue;
					me.placingTerrain = VariableCollection.nothing;
				} else if (recentBrushes.get(i) == VariableCollection.door_blue) {
					me.placingBlock = VariableCollection.door_blue;
					me.placingTerrain = VariableCollection.nothing;
				} else if (recentBrushes.get(i) == VariableCollection.key_yellow) {
					me.placingBlock = VariableCollection.key_yellow;
					me.placingTerrain = VariableCollection.nothing;
				} else if (recentBrushes.get(i) == VariableCollection.door_yellow) {
					me.placingBlock = VariableCollection.door_yellow;
					me.placingTerrain = VariableCollection.nothing;
				} else if(recentBrushes.get(i) == VariableCollection.EditorEraser) {
					me.placingBlock = VariableCollection.EditorEraser;
					me.placingTerrain = VariableCollection.nothing;
				} else if (recentBrushes.get(i) == VariableCollection.pillar_blue) {
					me.placingBlock = VariableCollection.nothing;
					me.placingTerrain = VariableCollection.pillar_blue;
				} else if (recentBrushes.get(i) == VariableCollection.pillar_yellow) {
					me.placingBlock = VariableCollection.nothing;
					me.placingTerrain = VariableCollection.pillar_yellow;
				} else if (recentBrushes.get(i) == VariableCollection.button_blue) {
					me.placingBlock = VariableCollection.nothing;
					me.placingTerrain = VariableCollection.button_blue;
				} else if (recentBrushes.get(i) == VariableCollection.button) {
					me.placingBlock = VariableCollection.nothing;
					me.placingTerrain = VariableCollection.button;
				}
				setBrush(recentBrushes.get(i));
				break;
			}	
		}
		StaticSprites.ghmViewbar.setPosition(this.x+background.getWidth()-StaticSprites.ghmViewbar.getWidth()-20, this.y+50);
		if (bR.contains(x,y)) {
			renderingBlocks = !renderingBlocks;
			me.renderingBlocks = renderingBlocks;
		} else if (tR.contains(x,y)) {
			renderingTerrain = !renderingTerrain;
			me.renderingTerrain = renderingTerrain;
		} else if (StaticSprites.wallEdges.getBoundingRectangle().contains(x,y)) {
			walledEdges = !walledEdges;
			me.wallEdges(walledEdges, me.map.rows, me.map.cols);
		} else if (StaticSprites.ghmViewbar.getBoundingRectangle().contains(x,y)) {
			setBrush(VariableCollection.CharDummy);
			me.placingBlock = VariableCollection.CharDummy;
			me.placingTerrain = VariableCollection.nothing;
		}
	}
	public void setBrush(int b) {
		System.out.println("BRUSH = " + b);
		boolean found = false;
		for (int i = 3; i >= 0; i--) {
			if (recentBrushes.get(i) == b) {
				recentBrushes.removeIndex(i);
				recentBrushes.add(b);
				found = true;
				break;
			}
		}
		if (!found) {
			recentBrushes.removeIndex(0);
			recentBrushes.add(b);
		}
		for (int i = 0; i < 4; i++) {
			if (recentBrushes.get(i) == VariableCollection.grass) {
				recentBrushesSprites[i] = StaticSprites.ctbSprites[StaticVariables.grassVal];
			} else if (recentBrushes.get(i) == VariableCollection.hole) {
				recentBrushesSprites[i] = StaticSprites.ctbSprites[StaticVariables.holeVal];
			} else if (recentBrushes.get(i) == VariableCollection.dirt) {
				recentBrushesSprites[i] = StaticSprites.ctbSprites[StaticVariables.dirtVal];
			} else if (recentBrushes.get(i) == VariableCollection.wall) {
				recentBrushesSprites[i] = StaticSprites.cbbSprites[StaticVariables.wallVal];
			} else if (recentBrushes.get(i) ==  VariableCollection.moveBlock) {
				recentBrushesSprites[i] = StaticSprites.cbbSprites[StaticVariables.moveVal];
			} else if (recentBrushes.get(i) ==  VariableCollection.boulder) {
				recentBrushesSprites[i] = StaticSprites.cbbSprites[StaticVariables.boulderVal];
			} else if (recentBrushes.get(i) ==  VariableCollection.door_red) {
				recentBrushesSprites[i] = StaticSprites.cbbSprites[StaticVariables.reddoorVal];
			} else if (recentBrushes.get(i) ==  VariableCollection.key_red) {
				recentBrushesSprites[i] = StaticSprites.cbbSprites[StaticVariables.redkeyVal];
			} else if (recentBrushes.get(i) ==  VariableCollection.EditorEraser) {
				recentBrushesSprites[i] = StaticSprites.cbbSprites[StaticVariables.eraserVal];
			} else if (recentBrushes.get(i) ==  VariableCollection.goal) {
				recentBrushesSprites[i] = StaticSprites.cbbSprites[StaticVariables.goalVal];
			} else if (recentBrushes.get(i) == VariableCollection.CharDummy) {
				recentBrushesSprites[i] = StaticSprites.ghmViewbar;
			} else if (recentBrushes.get(i) == VariableCollection.mergeblock) {
				recentBrushesSprites[i] = StaticSprites.cbbSprites[StaticVariables.mergeVal];
			} else if (recentBrushes.get(i) == VariableCollection.door_blue) {
				recentBrushesSprites[i] = StaticSprites.cbbSprites[StaticVariables.bluedoorVal];
			} else if (recentBrushes.get(i) == VariableCollection.key_blue) {
				recentBrushesSprites[i] = StaticSprites.cbbSprites[StaticVariables.bluekeyVal];
			} else if (recentBrushes.get(i) == VariableCollection.door_yellow) {
				recentBrushesSprites[i] = StaticSprites.cbbSprites[StaticVariables.yellowdoorVal];
			} else if (recentBrushes.get(i) == VariableCollection.key_yellow) {
				recentBrushesSprites[i] = StaticSprites.cbbSprites[StaticVariables.yellowkeyVal];
			} else if (recentBrushes.get(i) == VariableCollection.pillar_blue) {
				recentBrushesSprites[i] = StaticSprites.ctbSprites[StaticVariables.bluepillarVal];
			} else if (recentBrushes.get(i) == VariableCollection.pillar_yellow) {
				recentBrushesSprites[i] = StaticSprites.ctbSprites[StaticVariables.yellowpillarVal];
			} else if (recentBrushes.get(i) == VariableCollection.button_blue) {
				recentBrushesSprites[i] = StaticSprites.ctbSprites[StaticVariables.bluebuttonVal];
			} else if (recentBrushes.get(i) == VariableCollection.button) {
				recentBrushesSprites[i] = StaticSprites.ctbSprites[StaticVariables.yellowbuttonVal];
			}
		}
	}
	public void render(SpriteBatch sb) {
		background.setPosition(x,y);
		background.draw(sb);
		recentBrushesSprites[3].setPosition(x+20,y+background.getHeight()-blocks.getHeight()-recentBrushesSprites[3].getHeight()-40);
		recentBrushesSprites[3].draw(sb);
		blocks.setPosition(x+background.getWidth()-blocks.getWidth()-20, Gdx.graphics.getHeight()-blocks.getHeight()-20);
		terrain.setPosition(x+20, Gdx.graphics.getHeight()-terrain.getHeight()-20);
		StaticSprites.wallEdges.setPosition(x+20, y+50);
		StaticSprites.ghmViewbar.setPosition(x+background.getWidth()-StaticSprites.ghmViewbar.getWidth()-20, y+50);
		StaticSprites.ghmViewbar.draw(sb);
		if (walledEdges)
			StaticSprites.wallEdges.draw(sb,0.5f);
		else
			StaticSprites.wallEdges.draw(sb);	
		if (!renderingBlocks)
			blocks.draw(sb,0.5f);
		else
			blocks.draw(sb);
		if (!renderingTerrain)
			terrain.draw(sb,0.5f);
		else
			terrain.draw(sb);
		for (int i = 2; i >= 0; i --) {
			recentBrushesSprites[i].setBounds(x+100-i*40, y+background.getHeight()-blocks.getHeight()-recentBrushesSprites[3].getHeight()*2-20, 32, 32);
			recentBrushesSprites[i].draw(sb);
			recentBrushesSprites[i].setBounds(x+100-i*40, y+background.getHeight()-blocks.getHeight()-recentBrushesSprites[3].getHeight()*2-20, 64, 64);
		}
	}
}
